* Clean up 

clear all

/*
* Set working directory: please set your own
cd "/Users/vicentevalentim/Desktop/fragmentation_replication_bjps"
*/

** Import dataset
use Data/fragmentation_partylevel.dta, clear

* Calculate some necessary variables
gen year_sq = year ^ 2
egen median_year = median(year)
gen year_above_median = 0
replace year_above_median = 1 if year > median_year
rdrobust year_above_median distance_threshold_prop, vce(cluster country)
gen year_above_1989 = 0
replace year_above_1989 = 1 if year > 1989
rdrobust year_above_1989 distance_threshold_prop, vce(cluster country)

ta countryname, gen (country_)

* Generate local with placebo outcomes
local placebo_outcomes year year_above_1989 ///
country_1 country_2 country_3 country_4 country_5 country_6 country_7 ///
country_8 country_9 country_10 country_11 country_12 country_13 country_14 ///
country_15 country_16 country_17 country_18 country_19 country_20 country_21 ///
country_22 country_23 country_24 country_25 country_26 country_27 country_28 ///
country_29 country_30 country_31 country_32 country_33 country_34 country_35 ///
country_36 country_37 country_38 rile

* Standardize these outcomes
foreach outcome in `placebo_outcomes'{
egen `outcome'_avg = mean(`outcome')
egen `outcome'_sdv = sd(`outcome')
gen `outcome'_stnd = (`outcome' - `outcome'_avg) / `outcome'_sdv

}

* Generate excel to store results
putexcel set Results/placebos.xlsx, replace

* Generate a number to identify the iterations
local i = 2

	putexcel A1 = "id"
	putexcel B1 = "coefficient"	 
	putexcel C1 = "std_error"	 
	putexcel D1 = "outcome"	 
	putexcel E1 = "bandwidth"	

* Using the bandwidth used in the main analyses in all regressions (50 percent above threshold)
foreach outcome in `placebo_outcomes'{
rdrobust `outcome'_stnd distance_threshold_prop, vce(cluster country) h(0.5)


	* Store coefficient and standard error
	local coef1 = e(tau_cl)
	local se1 = e(se_tau_cl)

	* Save to excel, along with information on the model
	putexcel A`i' = `i' 
	putexcel B`i' = `coef1'	 
	putexcel C`i' = `se1'	 
	putexcel D`i' = "`outcome'"	 
	putexcel E`i' = "Bandwidth used in the main analyses (50% around threshold)"	 
	 
	local ++i
}

* Using optimal bandwidth in each regression
foreach outcome in `placebo_outcomes'{
rdrobust `outcome'_stnd distance_threshold_prop, vce(cluster country) 

	* Store coefficient and standard error
	local coef2 = e(tau_cl)
	local se2 = e(se_tau_cl)

	* Save to excel, along with information on the model
	putexcel A`i' = `i' 
	putexcel B`i' = `coef2'	 
	putexcel C`i' = `se2'	 
	putexcel D`i' = "`outcome'"	 	 
	putexcel E`i' = "Optimal bandwidth for each RDD"	 
	 
	local ++i
}

* Check country names
forvalues x = 1/38{
	ta country if country_`x' == 1
}

